﻿using System;
using System.Linq;
using System.Web.UI.WebControls;
using HiTeachers.Entities;
using HiTeachers.Services;
using HiTeachers.Utilities;

namespace HiTeachers.Web
{
    public partial class AdminFaqs : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack) return;

            mtvFaQs.SetActiveView(vListFaQ);
        }

        protected void BtnAddGuideClick(object sender, EventArgs e)
        {
            mtvFaQs.SetActiveView(vAdjustFaQ);

            ClearInputFields();
            Page.CloseMessage(lblMsg);
        }

        protected void BtnDeleteClick(object sender, EventArgs e)
        {
            if (Request.Form["chkItem"] == null) return;

            string selectedItems = Request.Form["chkItem"];
            string[] selectedItemIds = selectedItems.Split(',');
            foreach (StaticPage staticPage in
                selectedItemIds.Select(t => new Guid(t)).Select(itemId => StaticPageServices.GetById(itemId))) StaticPageServices.Delete(staticPage);

            gridData.DataBind();
            udpGrid.DataBind();
        }

        protected void GridDataRowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (!"Edit".Equals(e.CommandName)) return;

            if (e.CommandArgument == null) return;

            var itId = new Guid(Convert.ToString(e.CommandArgument));
            StaticPage staticPage = StaticPageServices.GetById(itId);

            txtTitle.Text = staticPage.Title;
            txtContent.Text = staticPage.Content;
            hdfContentId.Value = staticPage.ID.ToString();
            txtSortIndex.Text = staticPage.SortIndex.ToString();

            Page.CloseMessage(lblMsg);
            mtvFaQs.SetActiveView(vAdjustFaQ);
        }

        protected void BtnSaveClick(object sender, EventArgs e)
        {
            if (!Page.IsValid) return;
            try
            {
                StaticPage staticPage;
                if (string.IsNullOrEmpty(hdfContentId.Value))
                {
                    staticPage = new StaticPage
                    {
                        Title = txtTitle.Text,
                        Content = txtContent.Text,
                        ContentType = Entities.ContentType.FaQs,
                        SortIndex = string.IsNullOrEmpty(txtSortIndex.Text) ? 0 : Convert.ToInt32(txtSortIndex.Text),
                    };
                }
                else
                {
                    staticPage = StaticPageServices.GetById(new Guid(hdfContentId.Value));

                    staticPage.Title = txtTitle.Text;
                    staticPage.Content = txtContent.Text;
                    staticPage.SortIndex = string.IsNullOrEmpty(txtSortIndex.Text) ? 0 : Convert.ToInt32(txtSortIndex.Text);
                }

                StaticPageServices.SaveOrUpdate(staticPage);
                Page.DisplayMessage(lblMsg, "Cập nhật thành công!", MessageType.Success);
                btnCancel.Text = "Đóng";
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                Page.DisplayMessage(lblMsg, "Cập nhật không thành công!", MessageType.Error);
            }
        }

        protected void BtnCancelClick(object sender, EventArgs e)
        {
            ClearInputFields();
            gridData.DataBind();
            mtvFaQs.SetActiveView(vListFaQ);
        }

        #region Private Methods

        private void ClearInputFields()
        {
            txtTitle.Text = string.Empty;
            txtContent.Text = string.Empty;
            txtSortIndex.Text = string.Empty;
            hdfContentId.Value = string.Empty;
        }
        #endregion
    }
}
